package com.eric.rabbit.entity.direct;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @author:Eric
 * @date:2021/6/28
 */
@Slf4j
@Data
public class DirectSender {

    @Autowired
    RabbitTemplate rabbitTemplate;

    private static final String exchangeName = "direct.exchange";
    private static final String sameKeyExchangeName = "sameKey.direct.exchange";


    private final String[] keys = {"red", "yellow", "blue"};

    public void send(int i) {
        String key = keys[i % 3];
        String message = "direct:" + key + ":" + i;
        rabbitTemplate.convertAndSend(exchangeName, key, message);
        log.info("[x] sent {} ", message);
    }

    public void sendSameKey(int i) {
        String key = keys[i % 3];
        String message = "direct:" + key + ":" + i;
        rabbitTemplate.convertAndSend(sameKeyExchangeName, key, message);
        log.info("[x] sent {} ", message);
    }
}
